﻿@using Kevin.Shop.Business.Service.Entity.Helper
@using RiderShop.Mobile.ServiceEntity.Purchase
@model  RiderShop.Mobile.Modle.Purchase.CheckOutModel

@foreach (var item in Model.CartInfo.ProductList)
{
    <div class="groupbox">
        <div class="OtherTitle">
            <div class="TitleLeft"><span class="TitleLeftSpan">商品:@item.ProductId</span></div>
            <div class="TitleRight">
                <div name="EditQtyAfter" class="ItemQtySpan">
                    <div class="ItemQty">
                        <span class="qtyLable">数量：</span>
                        <div id="Qty_11-119-225" class="QtyTable">@item.Quantity</div>
                    </div>
                </div>
            </div>
        </div>
        <div class="ItemTable">
            <div id="OtherProductRight" class="ItemTableRight">
                <div class="OtherProductItem">
                    <div class="ItemLeft">
                            @if (item.ProductId.Trim() == "COM")
                            {
                                <img src="http://dev.rongji365.com/Storage/CommResource/COM/COM_100.jpg" alt="" />
                            }
                            else
                            {
                                <img src="@item.ImageName" alt="" />
                            }
                    </div>
                    <div class="ItemRight"><span class="ItemRightWrod">@item.Title</span></div>
                    <br class="clearfix">
                </div>
                <div class="ProductPrice"><span id="prd_price_000" class="Price">@item.Quantity x @item.UnitPrice = ￥@item.SubTotal</span> </div>
            </div>
        </div>
    </div>
}
<form id="submitOrder" method="POST" action="~/Purchase/SubmitOrder">
    <div class="groupbox">
        @if (WebClientContext.Current.OnLinePayment != PaymentType.PointPay)
        {

            <div class="OtherTitle">
                <div class="TitleLeft mt10 f15">收货地址</div>
            </div>
            <div class="ItemTable">
                <div class="OtherProductItem">
                    @if (!Model.NeedAddShippingAddress)
                    {
                        <label for="select-choice-a" class="select">选择收货地址</label>
                        <select name="ShippingId" id="ShippingId">
                            @foreach (var item in Model.AllShipingAddresses)
                            {
                                <option value="@item.ShippingId">@item.ShipTo-@item.CellPhone-@item.Address</option>
                            }
                        </select>
                    }
                    <a href="~/MyAccount/ShippingAddressCreate?action=order" data-role="button" >添加新地址</a>

                </div>
            </div>

	    <div class="OtherTitle">
                <div class="TitleLeft mt10 f15"><i class="glyphicon glyphicon-chevron-right"></i> 配送方式</div>
            </div>
            <div class="ItemTable">
                <div class="OtherProductItem">
                    <select id="ShippingModeId" name="ShippingModeId">
                        <option value="">请选择配送方式</option>
                        @foreach (var item in Model.OrderShippingType)
                        {
                            <option value="@item.ShippingModeId">@item.ShippingModeName</option>
                        }
                    </select>
                </div>
            </div>
        }

        <div class="OtherTitle">
            <div class="TitleLeft mt10 f15"><i class="glyphicon glyphicon-chevron-right"></i> 支付方式</div>
        </div>
        <div class="ItemTable">
            <div class="OtherProductItem">
                <input type="hidden" id="txtPayType" value="@WebClientContext.Current.OnLinePayment"/>
                @if (WebClientContext.Current.OnLinePayment != PaymentType.PointPay)
                {
                    <div>
                        <fieldset data-role="controlgroup">
                            <legend>选择付款方式</legend>
                            <input id="ridPaymentTypeArrivePay" name="ArrivePay" value="1" type="radio">
                            <label for="ridPaymentTypeArrivePay">货到付款</label>
                            <input id="ridPaymentTypeOnlinePay" name="ArrivePay" value="0" type="radio">
                            <label for="ridPaymentTypeOnlinePay">在线付款</label>
                        </fieldset>
                    </div>
                }
                @if (WebClientContext.Current.OnLinePayment == PaymentType.PointPay || WebClientContext.Current.OnLinePayment == PaymentType.AllPayment)
                {
                    <div id="checkPoint" class="ui-grid-a">
                        <div class="ui-block-a mt5">
                             <h3 class="f15 mt10">您当前积分:<span id="currentPoint" class="f18 red">@Model.UserCurrentPoint</span></h3>
                        </div>
                        <div class="ui-block-b">
                            <label class="fl mt10 mr5" >本次使用:</label> 
                            <input id="pointPay" class="fr mt10" type="number" maxlength="7" value="@Model.CartInfo.PayAmount" min="0" max="99999" data-type="range" /> 
                        </div>
                    </div>
                }
            </div>
        </div>
    </div>

    <div data-role="collapsible-set">
        <div data-role="collapsible">
            <h2>订单备注</h2>
            <input type="text" placeholder="限40字以内" class="intxt" maxlength="40" id="UserMemo" name="UserMemo" />
        </div>
    </div>
</form>
<div class="checkoutDiscount tline">
    <div class="total">
        <div class="totalinfo">
            <p><span class="tit">小计：</span><span class="case">@Model.CartInfo.ProductPriceAmount</span></p>
            <p><span class="tit">运费金额：</span><span id="ShippingCharge" class="case">@Model.CartInfo.ShippingCharge</span></p>
            <p><span class="tit">折扣小计：</span><span id="DiscountAmount" class="case green">- @Model.CartInfo.DiscountAmount</span></p>
            <p><span class="tit">积分抵扣：</span><span id="ExpenseAmount" class="case green">- @Model.CartInfo.ExpenseAmount</span></p>
            <p class="all"><span class="tit">应付金额：</span><span id="PayAmount">@Model.CartInfo.PayAmount </span></p>
        </div>
    </div>
</div>
<form action="~/Purchase/OrderCompleted" method="POST" id="toCompleted">
    <a href="#" data-role="button" id="submitorder" data-theme="f">提交订单</a>
</form>
<form action="~/Purchase/OrderBalanceCompute" method="POST" id="orderBalanceCompute"></form>
<script language="javascript" type="text/javascript">
    var orderData = @Html.Raw(Model.OrderInfoJson);  
    var multiSubmit = 0;
    $("#submitorder").click(function () {
        submitOrder();
    });
    $("#ShippingModeId").change(function(){
        BalanceCompute();
    });
    $("#ShippingId").change(function(){
        BalanceCompute();
    });
    submitOrder = function () {
        if(multiSubmit == 1)
        {
            return false;
        } 
        //检查
        KevinShop.Common.ShoppingCart.RefreshTotalQty();
        if (KevinShop.Common.ShoppingCart.TotalQty == 0) {
            KevinShop.MessageBox.Alert("购物车至少得有一个商品。");
            return false;
        }
        //var points = $('#usingPoints').val();
        //if(validPoint != points) {
        //    KevinShop.MessageBox.Alert("当您想使用账户余额抵扣订单金额，别忘了点击旁边的“使用”按钮，方能生效，谢谢。");
        //    return;
        //}
        if ($("input[name='ShippingId']").attr("value") == "") {
            KevinShop.MessageBox.Alert("请选择收货人。");
            return false;
        }
        if ($("select[name='ShippingModeId'] option:selected").val() == "") {
            KevinShop.MessageBox.Alert("请选择配送方式。");
            return false;
        }
        var payType = $("#txtPayType").val();
        if (payType == "PointPay" || payType == "AllPayment") {
            var point = Number($("#pointPay").val());
            var current = Number($("#currentPoint").text());

            if (point <= 0 || point == NaN) {
                KevinShop.MessageBox.Alert("积分必须大于0，请检查。");
                return false;
            }
            else if (point > 0 && point > current) {
                KevinShop.MessageBox.Alert("您的积分不足。");
                return false;
            }
            orderData.OrderExpensePoint =  Number($("#pointPay").val());
  
        } else  if ($("input[name='ArrivePay']:checked").length == 0) {
                KevinShop.MessageBox.Alert("请选择支付方式。");
                return false;
       }
    
        //if ($('#IsInvoice').attr("checked")) {
        //    if ($('#InvoiceTitle').val() == "") {
        //        KevinShop.MessageBox.Alert("请填写发票抬头。");
        //        return;
        //    }
        //}            
        //获取form表单
        
        var order = KevinShop.Common.FormToObj($('#submitOrder'), orderData);
  
        var url = document.forms["submitOrder"].action;
        KevinShop.Common.Ajax.Post(url,order, function (data) {
            if (data.error) {
                alert(data.error);
                return;
            }
            if (!data.Status) {

                KevinShop.MessageBox.Alert(data.ErrorMessage);
                return;
            }
            var completeUrl = document.forms["toCompleted"].action;
            window.location = completeUrl +"?par="+data.Tag;
        },false);
        multiSubmit = 0;
        return false;
    };
    var RefreshGiftCardList = function (html) {
        var div = $('#giftCardList');
        $(div).empty();
        $(div).html(html);
        //刷新订单金额
        Kevin.AjaxMethod.RefreshAmount(function (data) {
            RefreshOrderAmount(data);
        });
    };

    var BalanceCompute = function () {
        if ($("#ShippingId") && $("#ShippingId").val().length != 0
            && $("select[name='ShippingModeId'] option:selected").val().length != 0) 
        {
            var shiptype = $("select[name='ShippingModeId'] option:selected").val();
            var shippingId = $("#ShippingId").val();
            var orderType = [shiptype, shippingId]; 
            var url = document.forms["orderBalanceCompute"].action;
            //int shiptype, int shippingId
            url = url + "?shiptype=" + shiptype + "&shippingId=" + shippingId;
            KevinShop.Common.Ajax.Get(url, function (data) {
                RefreshOrderAmount(data);
            });
        }
    };
    BalanceCompute();   
    var RefreshOrderAmount = function (data) {
        if (!data) {
            return;
        }
        $('#ShippingCharge').text(data.ShippingCharge.toFixed(2));
        $('#DiscountAmount').text(data.DiscountAmount.toFixed(2));
        $('#PayAmount').text(data.PayAmount.toFixed(2));
        $('#GiftCardAmount').text(data.GiftCardAmount.toFixed(2));
        $('#ExpenseAmount').text(data.ExpenseAmount.toFixed(2));  
    };

</script>
@section footer
{
    <script type="text/javascript" src="~/Utility/pages/pg_purchase.js"></script>
}